<?php

class UsersController extends AppController
{

  var $name = 'Users';  
  var $components = array('context');
  var $helpers = array('context', 'Html', 'form');
  
  function login()
  {
    $this->layout="admin";
    $this->set('error', false);
  
    if (!empty($this->data))
    {
      $someone = $this->User->findByUsername($this->data['User']['username']);

      // Compare the MD5 encrypted version of the password against recorded encrypted password.
      if(!empty($someone['User']['password']) &&
      ($someone['User']['password'] == md5($this->data['User']['password'])))
      {
        $this->Session->write('User', $someone['User']);
        $this->redirect('/admin/content_pages/');
      }
      else
      {
        $this->set('error', true);
      }
    }
  }

  function admin_login()
  {
    // Redirect to the public login page if session has timed out.
    $this->redirect('/admin');
  }

  function admin_index() {
    $this->context->method='index';
    $this->context->checkSession($this);
    $this->layout="admin";
    
    $users=$this->User->findAll();
    $this->set('users', $users);
  }

  function admin_add() {
    $this->context->method='add';
    $this->context->checkSession($this);
    $this->layout="admin";

    $this->set('error', false);

    if (!empty($this->data)) 
    {
      // Check if the password and "verify password" match.
      if(!empty($this->data['User']['password']) &&
        $this->data['User']['password'] == $this->params['form']['validate_password']) 
      {
        // Encrypt the password prior to storing.
        $this->data['User']['password'] = md5($this->data['User']['password']);
        if ($this->User->save($this->data)) 
        {
          $this->redirect('/admin/users/');
        }
        else
        {
          $this->data['User']['password']='';
        }
      }
      else
      {
        $this->data['User']['password']='';
        $this->set('error', true);
      }
    }
  }

  function admin_edit($id = null) {
    $this->context->checkSession($this);
    $this->layout="admin";

    if (empty($this->data)) {
      $this->User->id = $id;
      $this->data = $this->User->read();
      $this->set('user', $this->User->read());
    }
    else
    {
      if ($this->User->save($this->data['User'])) {
        $this->redirect('/admin/users/');
      }
    }    
  }

  function admin_change_password($id = null)
  {
    $this->layout="admin";
    $this->set('error', false);
  
    if (empty($this->data))
    {
      $this->User->id = $id;
      $this->data = $this->User->read();
      // Clear password field so as not to enter the MD5 encrypted password value into the form.
      $this->data['User']['password']='';
      $this->set('user', $this->User->read());
    }
    else
    {
      if(!empty($this->data['User']['password']) &&
      $this->data['User']['password'] == $this->params['form']['validate_password'])
      {
        $this->data['User']['password'] = md5($this->data['User']['password']);
        if ($this->User->save($this->data['User']))
        {
          $this->redirect('/admin/users/');
        }
        else
        {
          $this->render();
        }
      }
      else
      {
        $this->set('error', true);
      }
    }
  }

  function admin_delete($id = null) {
    $this->context->checkSession($this);
    $this->layout="admin";

    if (empty($this->data)) {
    	
      $this->User->id = $id;
      $this->data = $this->User->read($id);
      $this->set('user', $this->User->read());
    }
    else
    {
      $this->User->del($this->data['User']['id']);
      $this->redirect('/admin/users/');
    }
  }

  function admin_logout()
  {
    $this->layout="admin";

    $this->Session->delete('User');
    $this->redirect('/admin/');
  }




  function add() {
    $this->context->method='add';
    $this->set('error', false);

    if (!empty($this->data)) 
    {
      // Check if the password and "verify password" match.
      if(!empty($this->data['User']['password']) &&
        $this->data['User']['password'] == $this->params['form']['validate_password']) 
      {
        // Encrypt the password prior to storing.
        $this->data['User']['password'] = md5($this->data['User']['password']);
        if ($this->User->save($this->data)) 
        {
          $this->redirect('/admin/users/');
        }
        else
        {
          $this->data['User']['password']='';
        }
      }
      else
      {
        $this->data['User']['password']='';
        $this->set('error', true);
      }
    }
  }
  
}
  
?>